package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PubSysUserBean;
import com.koron.css2.baseConfig.mapper.PubSysUserMapper;
import com.koron.css2.meterService.bean.query.MsChgPlanQueryBean;
import com.koron.css2.meterService.dto.MsChgPlanDto;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 换表查询
 * @author 周利斌
 *
 */
public class MsChgList implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			MsChgPlanQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsChgPlanQueryBean.class);
			if(bean.getReceiptType() == null) {
				return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "拆换表类型不能为空", Void.class);
			}
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			MsChgPlanMapper mapper = factory.getMapper(MsChgPlanMapper.class);	
			PubSysUserMapper userMapper = factory.getMapper(PubSysUserMapper.class);
			List<MsChgPlanDto> list = mapper.selectMsChgPlanList(bean);
			Map<String, String> replaceTypes = null;
			if(Objects.equals(2, bean.getReceiptType())) { //单据类型为拆表
				replaceTypes = Tools.mapDicByCode(factory, "MRM");//拆表类型
			}else if(Objects.equals(1, bean.getReceiptType())) { //单据类型为换表
				replaceTypes = Tools.mapDicByCode(factory, "MCM"); //换表类型
			}
			for(MsChgPlanDto t : list) {
				t.setReplaceTypeName(replaceTypes.get(t.getReplaceType()));
				t.setProcessStateName(Tools.transProcessStatus(t.getProcessState()));
				t.setReceiptTypeName(Objects.equals("1", t.getReceiptType()) ? "换表" : "拆表");
				PubSysUserBean user = userMapper.getUserByLoginName(t.getReplaceStaff());
				if(user != null) {
					t.setReplaceStaffName(user.getUserName());
				}
			}
			info.setData(new PageInfo<MsChgPlanDto>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
